package com.example.test622;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import java.util.Map;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.android.AndroidDisplayer;
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer;

public class MyBaseCache extends BaseCacheStuffer {
    private float LEFTMARGE;
    private float RIGHTMARGE;
    private float TEXT_SIZE;
    private float IMAGE_HEIGHT;

    public  MyBaseCache(Activity activity) {
        LEFTMARGE = activity.getResources().getDimension(R.dimen.dp_21);
        RIGHTMARGE =  activity.getResources().getDimension(R.dimen.dp_23);
        TEXT_SIZE =  activity.getResources().getDimension(R.dimen.dp_26);
        IMAGE_HEIGHT =  activity.getResources().getDimension(R.dimen.dp_19);
    }
    @Override
    public void measure(BaseDanmaku danmaku, TextPaint paint, boolean fromWorkerThread) {
        Map<String, Object> map = (Map<String, Object>) danmaku.tag;
        String content = (String) map.get("content");
        Bitmap bitmap = (Bitmap) map.get("bitmap");
        paint.setTextSize(TEXT_SIZE);
        float contentWidth = paint.measureText(content);
        danmaku.paintWidth = contentWidth + LEFTMARGE + RIGHTMARGE+IMAGE_HEIGHT;
        danmaku.paintHeight = IMAGE_HEIGHT*1.2F;
    }

    @Override
    public void clearCaches() {

    }

    @Override
    public void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, float top, boolean fromWorkerThread, AndroidDisplayer.DisplayerConfig displayerConfig) {
        Map<String, Object> map = (Map<String, Object>) danmaku.tag;
        String content = (String) map.get("content");
        Bitmap bitmap = (Bitmap) map.get("bitmap");
        String color = (String) map.get("color");
        //画笔
        Paint paint = new Paint();
        paint.setTextSize(TEXT_SIZE);
        //背景
        int textLength = (int) paint.measureText(content);
        paint.setColor(Color.parseColor(color));
        //边框
        float frameLeft = left;
        float frameTop = top;
        float frameRight = left + textLength + LEFTMARGE + RIGHTMARGE+IMAGE_HEIGHT;
        float frameBottom = top + RIGHTMARGE;
        canvas.drawRoundRect(new RectF(frameLeft, frameTop, frameRight, frameBottom),IMAGE_HEIGHT/2,IMAGE_HEIGHT/2,paint);
        //头像
        float avatarRight = left+IMAGE_HEIGHT;
        float avatarBottom = top + IMAGE_HEIGHT;
        canvas.drawBitmap(bitmap, null, new RectF(left ,top, avatarRight, avatarBottom), paint);
        //文本
        paint.setColor(Color.WHITE);
        float contentLeft = left + IMAGE_HEIGHT+LEFTMARGE;
        //偏移量
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float textTop = fontMetrics.top;
        float textBottom = fontMetrics.bottom;
        float contentBottom = top+IMAGE_HEIGHT/2;
        int baseLineY = (int)(contentBottom-textTop/2-textBottom/2);
        //文本
        canvas.drawText(content,contentLeft,baseLineY,paint);
    }
}
